package com.manor.paradise.activity

import android.content.Context
import android.content.Intent
import android.view.View
import android.widget.ImageSwitcher
import android.widget.ImageView
import androidx.viewpager.widget.ViewPager
import com.manor.common.activity.AbsActivity
import com.manor.common.utils.DrawableUtil
import com.manor.common.utils.KLog
import com.manor.paradise.R
import com.manor.paradise.adapter.CommonPagerAdapter
import com.manor.paradise.fragment.*
import kotlinx.android.synthetic.main.activity_my_employment.*
import kotlinx.android.synthetic.main.activity_my_employment.im_back
import kotlinx.android.synthetic.main.activity_my_employment.ivQuest

/**
 * 精灵之家
 * @property tv_list ArrayList<ImageSwitcher>
 * @property older_int Int
 */
class MyEmploymentActivity : AbsActivity() {

    internal var tv_list: ArrayList<ImageSwitcher> = ArrayList()
    private var older_int = 0
    var item : Int = 0
    companion object {
        fun forward(context: Context, item: Int) {
            val intent = Intent(context, MyEmploymentActivity::class.java)
            intent.putExtra("item", item)
            context.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_my_employment

    }


    override fun main() {
        super.main()
        mContext = this
        item = intent?.extras?.getInt("item",0)?:0
        tv_list.add(tv_myFairy)
        tv_list.add(tv_ruleDescription)
        tv_list.add(tv_getHoney)
        tv_list.add(tv_employmentRecords)

        tv_gyjl.setOnClickListener {
            startActivity(Intent(this, MineActivity::class.java))
        }
        val pagerAdapter = CommonPagerAdapter(
                supportFragmentManager,
                MyFairyFragment.newInstance(),
                GetHoneyFragment.newInstance(),
                EmploymentRecordsFragment.newInstance(),
                RuleDescriptionFragment.newInstance()
        )
        viewpager?.offscreenPageLimit = tv_list.size
        viewpager?.adapter = pagerAdapter

        viewpager?.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
            override fun onPageSelected(position: Int) {
                setViewpagershow(position)
            }

            override fun onPageScrollStateChanged(state: Int) {
                super.onPageScrollStateChanged(state)
                KLog.e("tag", state.toString() + "")
            }
        })
        im_back.setOnClickListener { finish() }
        //showFragment(myFairy);
        tv_myFairy?.setOnClickListener {
            viewpager?.currentItem = 0

        }
        tv_ruleDescription?.setOnClickListener {
            viewpager?.currentItem = 1

        }
        tv_getHoney?.setOnClickListener {
            viewpager?.currentItem = 2

        }
        tv_employmentRecords?.setOnClickListener {
            viewpager?.currentItem = 3

        }

        tv_myFairy.setFactory {
            val iv = ImageView(this)
            iv.setImageResource(R.mipmap.pic_tab_x)
            iv
        }
        tv_getHoney.setFactory {
            val iv = ImageView(this)
            iv.setImageResource(R.mipmap.pic_tab_wx)
            iv
        }
        tv_employmentRecords.setFactory {
            val iv = ImageView(this)
            iv.setImageResource(R.mipmap.pic_tab_wx)
            iv
        }
        tv_ruleDescription.setFactory {
            val iv = ImageView(this)
            iv.setImageResource(R.mipmap.pic_tab_wx)
            iv
        }
        tv_myFairy.setInAnimation(this, android.R.anim.fade_in)
        tv_myFairy.setOutAnimation(this, android.R.anim.fade_out)
        tv_getHoney.setInAnimation(this, android.R.anim.fade_in)
        tv_getHoney.setOutAnimation(this, android.R.anim.fade_out)
        tv_employmentRecords.setInAnimation(this, android.R.anim.fade_in)
        tv_employmentRecords.setOutAnimation(this, android.R.anim.fade_out)
        tv_ruleDescription.setInAnimation(this, android.R.anim.fade_in)
        tv_ruleDescription.setOutAnimation(this, android.R.anim.fade_out)
        viewpager.currentItem = item
    }


    private fun setViewpagershow(tv: Int) {
        if (tv == 0) {
            tv_gyjl?.visibility = View.VISIBLE
        } else {
            tv_gyjl?.visibility = View.GONE
        }

        tv_list.get(tv).setImageResource(R.mipmap.pic_tab_x)
        tv_list.get(older_int).setImageResource(R.mipmap.pic_tab_wx)
        older_int = tv


    }





}
